package com.fw.payment.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.payment.service.ServiceExecutor;
import com.fw.payment.service.ServiceOrder;
import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.file.FileService;
import com.fw.service.file.bean.FileBean;
import com.fw.service.file.bean.SearchFileBean;

@Component
@Scope(SCOPE_PROTOTYPE)
public class FileBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private FileService fileService;

    public FileBean saveFile(String filename, String contentType, byte[] content, String description) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.fileService);
	    serviceOrder.setMethodName("saveFile");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class, byte[].class, String.class, });
	    serviceOrder.setParameterValues(new Object[] { filename, contentType, content, description });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		FileBean fileBean = (FileBean) serviceOrder.getReturnValue();
		return fileBean;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public FileBean saveFileAndGenerateFilename(String fileExtension, String contentType, byte[] content, String description)
	    throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.fileService);
	    serviceOrder.setMethodName("saveFileAndGenerateFilename");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class, byte[].class, String.class, });
	    serviceOrder.setParameterValues(new Object[] { fileExtension, contentType, content, description });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		FileBean fileBean = (FileBean) serviceOrder.getReturnValue();
		return fileBean;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public FileBean getFile(Long id) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.fileService);
	    serviceOrder.setMethodName("getFile");
	    serviceOrder.setParameterTypes(new Class[] { Long.class });
	    serviceOrder.setParameterValues(new Object[] { id });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		FileBean result = (FileBean) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public Boolean deleteFile(Long id) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.fileService);
	    serviceOrder.setMethodName("deleteFile");
	    serviceOrder.setParameterTypes(new Class[] { Long.class });
	    serviceOrder.setParameterValues(new Object[] { id });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		Boolean result = (Boolean) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public SearchResultBean<FileBean> searchFile(SearchCriteriaBean<SearchFileBean> searchCriteriaBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.fileService);
	    serviceOrder.setMethodName("searchFile");
	    serviceOrder.setParameterTypes(new Class[] { SearchCriteriaBean.class });
	    serviceOrder.setParameterValues(new Object[] { searchCriteriaBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		SearchResultBean<FileBean> result = (SearchResultBean<FileBean>) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

}
